﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-14 13:25:36
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq.Expressions;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{

    /// <summary>
    ///用户信息表 数据访问层
    /// </summary>	
    internal partial class User
    {

        /// <summary>
        /// 根据断言条件获取单个用户信息表
        /// </summary>
        public UserInfo QuerySingle(Expression<Func<UserInfo, bool>> predicate)
        {
            return SQLServerProvider.CreateObject(ConnectionStringKey, predicate);
        }

        /// <summary>
        /// 根据断言条件获获取用户信息表列表
        /// </summary>
        public IList<UserInfo> QueryList(Expression<Func<UserInfo, bool>> predicate)
        {
            return SQLServerProvider.CreateList(ConnectionStringKey, predicate);
        }

        /// <summary>
        /// 根据手机获取用户信息
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public UserInfo GetUserByMobile(string mobile)
        {
            Model.UserInfo info = null;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [User] ");
            strSql.Append("WHERE IsActive=1 AND [UserMobile]=@UserMobile ");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@UserMobile", DbType.String, mobile);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
        }

        /// <summary>
        /// 更新用户等级
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        public int UpdateUserLevel(long userID, int level)
        {
            Model.UserInfo info = null;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE [User] SET UserLevel=@UserLevel WHERE UserID=@UserID ");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@UserLevel", DbType.Int32, level);
            db.AddInParameter(dbc, "@UserID", DbType.Int64, userID);
            return db.ExecuteNonQuery(dbc);
        }

        /// <summary>
        /// 更新用户头像
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="userName"></param>
        /// <param name="headImg"></param>
        /// <returns></returns>
        public int UpdateUserHead(long userID, string userName, string headImg)
        {
            Model.UserInfo info = null;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE [User] SET UserName=@UserName,HeadImg=@HeadImg WHERE UserID=@UserID");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@UserName", DbType.String, userName);
            db.AddInParameter(dbc, "@HeadImg", DbType.String, headImg);
            db.AddInParameter(dbc, "@UserID", DbType.Int64, userID);
            return db.ExecuteNonQuery(dbc);
        }
    }

}